//
//  ViewController.m
//  20602JSON
//
//  Created by student on 16/3/25.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController.h"
#import "Video.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *list;

@end
//http://120.25.226.186:32812/video?type=JSON
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 1. 获取数据
    NSString *urlStr = @"http://120.25.226.186:32812/video?type=JSON";
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlStr]];// 二进制    NSError *error;
    NSError *error;
    NSDictionary *resultDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
    NSLog(@"%@",resultDict);

    
    //转换成模型放入数组
    NSArray *videosArr = resultDict[@"videos"];
    self.list = [NSMutableArray array];
    for (NSDictionary *dict in videosArr)
    {
        Video *v = [Video videoWithDict:dict];
        [self.list addObject:v];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
    }
    
    Video *v = self.list[indexPath.row];
    cell.textLabel.text = v.name;
    cell.detailTextLabel.text = v.url;
    
    NSString *imageUrlStr = [@"http://120.25.226.186:32812/" stringByAppendingString:v.image];
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrlStr]];
    UIImage *image = [UIImage imageWithData:imageData];
    cell.imageView.image = image;
    
    return cell;
}


@end
